﻿using PLCS.Shared.Common.Constant;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace PLCS.App.Shared.Views.UserControls.Chlidren
{
    /// <summary>
    /// Tunnel.xaml 的交互逻辑
    /// 立库巷道
    /// </summary>
    public partial class RowGoods : UserControl
    {
        #region Properties

        /// <summary>
        /// 巷道列数
        /// </summary>
        public int ColumnCount
        {
            get { return (int)GetValue(ColumnCountProperty); }
            set { SetValue(ColumnCountProperty, value); }
        }

        public static readonly DependencyProperty ColumnCountProperty =
            DependencyProperty.Register("ColumnCount", typeof(int), typeof(RowGoods), new PropertyMetadata(1));

        /// <summary>
        /// 当前巷道
        /// </summary>
        public int Roadway
        {
            get { return (int)GetValue(RoadwayProperty); }
            set { SetValue(RoadwayProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Roadway.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty RoadwayProperty =
            DependencyProperty.Register("Roadway", typeof(int), typeof(RowGoods), new PropertyMetadata(1));

        /// <summary>
        /// 当前排
        /// </summary>
        public int Row
        {
            get { return (int)GetValue(RowProperty); }
            set { SetValue(RowProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Roadway.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty RowProperty =
            DependencyProperty.Register("Row", typeof(int), typeof(RowGoods), new PropertyMetadata(1));

        /// <summary>
        /// 层数
        /// </summary>
        public int LayerCount
        {
            get { return (int)GetValue(LayerCountProperty); }
            set { SetValue(LayerCountProperty, value); }
        }

        // Using a DependencyProperty as the backing store for LayerCount.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty LayerCountProperty =
            DependencyProperty.Register("LayerCount", typeof(int), typeof(RowGoods), new PropertyMetadata(1));

        /// <summary>
        /// 无需显示的列
        /// </summary>
        public string NoDisplayColumn
        {
            get { return (string)GetValue(NoDisplayColumnProperty); }
            set { SetValue(NoDisplayColumnProperty, value); }
        }

        public static readonly DependencyProperty NoDisplayColumnProperty =
            DependencyProperty.Register("NoDisplayColumn", typeof(string), typeof(RowGoods), new PropertyMetadata(""));

        /// <summary>
        /// 名称
        /// </summary>
        public string SName
        {
            get { return (string)GetValue(SNameProperty); }
            set { SetValue(SNameProperty, value); }
        }

        public static readonly DependencyProperty SNameProperty =
            DependencyProperty.Register("SName", typeof(string), typeof(RowGoods), new PropertyMetadata("ST1"));

        /// <summary>
        /// 方向
        /// </summary>
        public string Platepos
        {
            get { return (string)GetValue(PlateposProperty); }
            set { SetValue(PlateposProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Platepos.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PlateposProperty =
            DependencyProperty.Register("Platepos", typeof(string), typeof(RowGoods), new PropertyMetadata("左"));

        public ICommand Command
        {
            get { return (ICommand)GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }
        }

        public static readonly DependencyProperty CommandProperty =
            DependencyProperty.Register("Command", typeof(ICommand), typeof(RowGoods), new UIPropertyMetadata(null));

        public object CommandParameter
        {
            get { return (object)GetValue(CommandParameterProperty); }
            set { SetValue(CommandParameterProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CommandParameter.  This enables animation,styling,binding,etc...
        public static readonly DependencyProperty CommandParameterProperty =
            DependencyProperty.Register("CommandParameter", typeof(object), typeof(RowGoods), new UIPropertyMetadata(null));

        /// <summary>
        /// 强制输入事件和属性来自FrameworkElement
        /// </summary>
        public IInputElement CommandTarget
        {
            get { return (IInputElement)GetValue(CommandTargetProperty); }
            set { SetValue(CommandTargetProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CommandTarget.  This enables animation,etc...
        public static readonly DependencyProperty CommandTargetProperty =
            DependencyProperty.Register("CommandTarget", typeof(IInputElement), typeof(RowGoods), new UIPropertyMetadata(null));

        #endregion Properties

        #region Constructor

        public RowGoods()
        {
            InitializeComponent();
            Cursor = Cursors.Hand; //小手
            SizeChanged += Tunnel_SizeChanged;
        }

        #endregion Constructor

        #region Method

        /// <summary>
        /// 重载鼠标左击时触发事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);
            //_eventAggregator.GetEvent<StationEvent>().Publish(SName);
            var command = Command;
            var parameter = CommandParameter;
            var target = CommandTarget;

            var routedCmd = command as RoutedCommand;
            if (routedCmd != null && routedCmd.CanExecute(parameter, target))
            {
                routedCmd.Execute(parameter, target);
            }
            else if (command != null && command.CanExecute(parameter))
            {
                command.Execute(parameter);
            }
        }

        /// <summary>
        /// Usercontrol size changed event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void Tunnel_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            this.LayoutCanvas.Children.Clear();
            this.LayoutCanvas.Width = RenderSize.Width;
            this.LayoutCanvas.Height = RenderSize.Height;

            #region 巷道

            List<string> ls_notAvailableColumn = new List<string>() { "0" };
            if (!string.IsNullOrEmpty(NoDisplayColumn)) ls_notAvailableColumn = NoDisplayColumn.Split(ConstantChar.SplitChar).ToList();
            if (ColumnCount <= 0) return;

            for (int i = 0; i < ColumnCount; i++)
            {
                //不显示的部分不画矩形
                if (ls_notAvailableColumn.Contains((i + 1).ToString())) continue;

                this.LayoutCanvas.Children.Add(new Rectangle()
                {
                    Width = this.LayoutCanvas.Width / ColumnCount,
                    Height = this.LayoutCanvas.Height,
                    Fill = Brushes.WhiteSmoke,
                    StrokeThickness = 0.5,
                    Stroke = Brushes.DimGray,
                    Margin = new()
                    {
                        Left = this.LayoutCanvas.Width * i / ColumnCount - 0.5
                    }
                });
                //数字显示
                if (i != 0 && (i + 1) % 5 != 0) continue;
                this.LayoutCanvas.Children.Add(new Viewbox()
                {
                    Width = this.LayoutCanvas.Width / ColumnCount,
                    Height = this.LayoutCanvas.Height,
                    Margin = new()
                    {
                        Left = this.LayoutCanvas.Width * i / ColumnCount
                    },
                    Child = new TextBlock()
                    {
                        Text = Platepos == "左" ? (i + 1).ToString() : (ColumnCount - i).ToString(),
                        FontSize = 10,
                        Foreground = Brushes.DimGray
                    }
                }); ;
            }

            #endregion 巷道
        }

        #endregion Method
    }
}